package com.qf.contract.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.ResultCode;
import com.qf.contract.entity.*;
import com.qf.contract.mapper.FContractMapper;
import com.qf.contract.mapper.FContractPaymentMapper;
import com.qf.contract.qo.FContractPaymentQo;
import com.qf.contract.service.FContractPaymentSerivce;
import com.qf.contract.vo.FContractPaymentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class FContractPaymentSerivceImpl implements FContractPaymentSerivce {
    @Resource
    private FContractPaymentMapper fContractPaymentMapper;
    @Resource
    private FContractMapper fContractMapper;

    /**
     * 添加合同支付信息
     *
     * @param fContractPaymentQo
     * @return
     */
    @Override
    public int addFContractPayment(FContractPaymentQo fContractPaymentQo) {
        try {
            FContractPayment fContractPayment = new FContractPayment();
            BeanUtils.copyProperties(fContractPaymentQo, fContractPayment);
            //设置默认时间
            fContractPayment.setContractPaymentDate(new Date());
            /**
             * 查询他欠多少钱
             */
//        FContract fContract = fContractMapper.selectById(fContractPaymentQo.getContractId());
        /*
        获取他的合同金额
         */
//        Integer contractAmount = fContract.getContractAmount();
            /**
             * 添加记录
             */
            return fContractPaymentMapper.insert(fContractPayment);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

    /**
     * 批量删除信息
     * * @return
     */
    @Override
    public int delContractPayment(List ids) {
        try {
            for (Object id : ids) {
                FContractRecycleBin fContractRecycleBin = new FContractRecycleBin();
                fContractRecycleBin.setRecoveredId((Integer) id);
            }
            return fContractPaymentMapper.deleteBatchIds(ids);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

    /**
     * 查询已支付信息
     *
     * @param fContractPaymentQo
     * @param current
     * @param size
     * @return
     */
    @Override
    public IPage<FContractPaymentVo> fContractPaymentSerivce(FContractPaymentQo fContractPaymentQo, int current, int size) {
        IPage<FContractPaymentVo> page;
        try {
            MPJLambdaWrapper<FContractPayment> wrapper = new MPJLambdaWrapper<FContractPayment>()
                    .selectAll(FContractPayment.class)
                    .selectAll(FContract.class)//查询user表全部字段
                    .selectAll(FContractDetails.class)
                    .selectAll(FTypeOfContract.class)
                    .selectAll(FSigningUnit.class)
                    .leftJoin(FContract.class,FContract::getContractId,FContractPayment::getContractId)
                    .leftJoin(FContractDetails.class, FContractDetails::getContractId, FContract::getContractDetails)
                    .leftJoin(FTypeOfContract.class, FTypeOfContract::getContractId, FContract::getTypeOfContract)
                    .leftJoin(FSigningUnit.class, FSigningUnit::getUnitId, FContract::getSigningUnit);
//        List<ContractRecordQueryOV> List = fContractMapper.selectJoinList(ContractRecordQueryOV.class, wrapper);
            //TODO 分页连表查询
            page = fContractPaymentMapper.selectJoinPage(new Page<>(current, size), FContractPaymentVo.class, wrapper);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
        return page;
    }

    /**
     * 数据恢复
     *
     * @return
     */
    @Override
    public int dataRecovery() {
        /**
         * 全部数据无条件恢复
         */
        try {
            return fContractPaymentMapper.dataRecovery(0);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }
}
